﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.ObjectBuilder;
using Microsoft.Practices.CompositeWeb;
using System.Web;
using System.Web.Security;
using AP.LUNA.BusinessLogic;
using AP.LUNA.Commons;
using AP.LUNA.DataEntities;

namespace AP.LUNA.Shell.Views
{
    public class LoginPresenter : Presenter<ILoginView>
    {

        // NOTE: Uncomment the following code if you want ObjectBuilder to inject the module controller
        //       The code will not work in the Shell module, as a module controller is not created by default
        //
        // private IShellController _controller;
        // public LoginPresenter([CreateNew] IShellController controller)
        // {
        // 		_controller = controller;
        // }
        private HttpContext context = null;
        private BLSeguridad objSeguridad = null;

        public LoginPresenter()
        {
            context = HttpContext.Current;
            objSeguridad = new BLSeguridad();
        }

        public override void OnViewLoaded()
        {
            // TODO: Implement code that will be executed every time the view loads
        }

        public override void OnViewInitialized()
        {
            // TODO: Implement code that will be executed the first time the view loads


        }

        // TODO: Handle other view events and set state in the view

        public void IniciarSesion()
        {
            if (Membership.ValidateUser(this.View.usuario, this.View.pwd))
            {
                my_aspnet_users usuario = objSeguridad.GetUserbyUser(this.View.usuario);
                context.Session.Add(Constanst.CREDENCIAL, usuario);
                FormsAuthentication.RedirectFromLoginPage(this.View.usuario, false);
            }
            else
                this.View.mensaje = "Login Fallido. Por favor verifique su usuario y contraseña.";
        }
    }
}




